<?php

/*
 * jzp2008
 */

namespace app\mall\controller;

use think\Controller;
use app\mall\model\GoodsModel;
//use Util\ModelUtils;
//use Util\Tree;

use think\db;

class goods extends Index {

    public function index() {
        
    }

    public function serach() {
        $input = Request()->param();
        $page = isset($input['page']) ? $input['page'] : '1';
        $perPage = isset($input['per_page']) ? $input['per_page'] : '6';
        $perPage > 30 && $perPage = 30;
        $keyword = isset($input['keyword']) ? $input['keyword'] : '';
        $cateid = isset($input['cate_id']) ? $input['cate_id'] : "";
        $orderby = isset($input['orderby']) ? $input['orderby'] : 'orderby';
        $direction = isset($input['direction']) ? $input['direction'] : 'ASC';
        $where = [];
        $where = ['audit' => 1, 'closed' => 0, 'delete_time' => 0];
        if ($keyword != '') {
            $where['title'] = ['like', "%{$keyword}%"];
        }
        if(!empty($cateid)){
            $where['cate_id'] = $cateid;
        }

        $count = DB('Goods')->where($where)->count();
        $data['goods'] = DB('goods')
                ->where($where)
                ->order('orderby','ASC','{$orderby}','{$direction}')
                ->page($page, $perPage)
                ->select();
        foreach ($data['goods'] as $key => $value) {
            $data['goods'][$key]['photo'] = explode(',', $value['photo']);
            $data['goods'][$key]['details'] = html_entity_decode($value['details']);
        }
        $data['pagination'] = pagination($count, $perPage, $page);
        return $data;
    }
    
    public function getDetail($id)
    {
        $goodsmoedel = new Goods();
        $where = [];
        $where['expired_at'] = [['eq', 0], ['egt', time()], 'or'];
        $where = ['audit' => 1, 'closed' => 0, 'delete_time' => 0];
        $goods = DB('Goods')->where($where)->find($id);
        
        $cate = DB('GoodsCate')->where(['cate_id'=>$goods['cate_id'],'delete_time'=>0])->find();
        $cate_father = DB('GoodsCate')->where(['cate_id'=>$cate['parent_id'],'delete_time'=>0])->find();       
        DB('Goods')->where(['goods_id' => $id])->setInc('views');
        
        $comments_count = DB('GoodsComment')->where(['goods_id'=>$goods['goods_id'],'is_checked'=>'Y'])->count();
        $goods['comments_count'] = $comments_count;
        $this->fillGoods($goods);
        return $goods;
    }
    
    private function fillGoods(&$goods)
    {
        $user = get_user();
        $attrs = DB('GoodsAttr')->where(['goods_id' => $goods['goods_id'], 'status' => '1'])->select();
        $attrs = list_keep($attrs, ['id', 'name', 'val']);


        $specs = DB('GoodsSpec')->where(['goods_id' => $goods['goods_id'], 'status' => '1'])->select();
        $specs = array_group_by($specs, 'name');
        foreach ($specs as &$spec) {
            $spec['items'] = list_keep($spec['items'], ['id', 'val']);
        }
        if ($goods['photo']) $goods['photo'] = explode(',', $goods['photo']);
        if ($goods['details']) $goods['details'] = html_entity_decode($goods['details']);

        $skus = DB('GoodsSku')->field('name,price,wholesale_price,stock,photo')->where(['goods_id' => $goods['goods_id']])->select();
        // 售价判断
        foreach ($skus as &$sku) {
            $sku['price'] = $user['role'] === '2' ? $sku['wholesale_price'] : $sku['price'];
            $sku['photo'] = !empty($sku['photo'])?explode(",",$sku['photo']):$goods['photo'][0];
            unset($sku['wholesale_price']);
        }
        $goods['attrs'] = $attrs;
        $goods['specs'] = $specs;
        $goods['skus'] = $skus;
    }
}
